package com.zxf.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;

import com.zxf.pojo.Users;
import java.io.Serializable;

import javax.annotation.Resource;
public class UserDAOImpl implements UserDao{
	
	@Resource
	    protected RedisTemplate<Serializable, Serializable> redisTemplate;

	    public boolean saveUser(final Users user) {
	        redisTemplate.execute(new RedisCallback<Object>() {

	            @Override
	            public Object doInRedis(RedisConnection connection) throws DataAccessException {
	                connection.set(redisTemplate.getStringSerializer().serialize("user.uid." + user.getUserid()),
	                               redisTemplate.getStringSerializer().serialize("user.name."+ user.getUsername()));
	                return null;
	            }

				
	        });
			return false;
	    }

	    @Override
	    public Users getUser(final Integer id) {
	        return redisTemplate.execute(new RedisCallback<Users>() {
	            @Override
	            public Users doInRedis(RedisConnection connection) throws DataAccessException {
	                byte[] key = redisTemplate.getStringSerializer().serialize("user.uid." + id);
	                if (connection.exists(key)) {
	                    byte[] value = connection.get(key);
	                    String name = redisTemplate.getStringSerializer().deserialize(value);
	                    Users user = new Users();
	                    //user.setUserid(id);
	                    user.setUsername(name);
	                    return user;
	                }
	                return null;
	            }
	        });
	    }

		@Override
		public boolean add(Users users) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean add(List<Users> list) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void delete(String key) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public Users get(String keyId) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Users getUser(long id) {
			// TODO Auto-generated method stub
			return null;
		}

}
